﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.UI.WebControls;
using Celeriq.Common;
using System.Web.UI.HtmlControls;
using PJB.FacetedNavigation;

namespace PJBWebsite.UserControls
{
    public partial class DimensionListControl : System.Web.UI.UserControl
    {
        #region Class Members

        #endregion

        #region Page Events

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            lstItem.ItemDataBound += new DataListItemEventHandler(lstItem_ItemDataBound);

        }

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
        }

        #endregion

        #region Methods

        public void Populate(ListingResults results)
        {
            lstItem.DataSource = results.DimensionList.OrderBy(x => x.Name);
            lstItem.DataBind();
        }

        #endregion

        #region Property Implementations

        #endregion

        #region Event Handlers

        private void lstItem_ItemDataBound(object sender, DataListItemEventArgs e)
        {
            var dimensionItem = e.Item.DataItem as DimensionItem;

            const int HIDE_LIMIT = 8;

            var pnlHeader = (PlaceHolder) e.Item.FindControl("pnlHeader");
            var linkHeader = new Literal();
            linkHeader.Text = "<img title=\"Hide/show this section\" src=\"/images/dim-down.png\" class=\"toggle\" expanded=\"1\" />&nbsp;" + dimensionItem.Name;
            pnlHeader.Controls.Add(linkHeader);
            var pnlInside = (Panel) e.Item.FindControl("pnlInside");
            if (linkHeader.Text == string.Empty)
            {
                this.Visible = false;
                return;
            }

            var hidePanel = new Panel();
            hidePanel.Style.Add("display", "none");

            var showMore = new Literal();

            //Create a template query for speed
            var templateQuery = new ListingQuery(this.Request.Url.AbsoluteUri);

            var index = 0;
            foreach (var refinementItem in dimensionItem.RefinementList.OrderBy(x => x.FieldValue))
            {
                var query = ((ICloneable) templateQuery).Clone() as ListingQuery;
                query.PageOffset = 1;

                var link = new HtmlAnchor();
                var rValue = refinementItem.FieldValue;
                var text = rValue;
                if (text.Length > 22)
                {
                    text = text.Substring(0, 19) + "...";
                }
                text += " <span class=\"countbubble\">" + refinementItem.Count + "</span>";
                link.InnerHtml = text;

                if (!query.DimensionValueList.Contains(refinementItem.DVIdx))
                    query.DimensionValueList.Add(refinementItem.DVIdx);

                link.HRef = query.ToString();
                link.Title = rValue + " (" + refinementItem.Count + " items)";
                link.Attributes.Add("asynch-load", "");

                var breaker = new Label();
                breaker.Text = "<br />";

                if (index < HIDE_LIMIT)
                {
                    pnlInside.Controls.Add(link);
                    pnlInside.Controls.Add(breaker);
                }
                else
                {
                    hidePanel.Controls.Add(link);
                    hidePanel.Controls.Add(breaker);
                }

                if (index == HIDE_LIMIT)
                {
                    pnlInside.Controls.Add(hidePanel);
                    pnlInside.Controls.Add(showMore);
                }

                index++;

            }

            showMore.Text = "<div align='right'><a href='javascript:;' class=\"noautoscroll\" onclick=\"" + "javascript:showMore('" + hidePanel.ClientID + "', this)" + "\">show more</a></div>";

        }

        #endregion

    }
}